package com.jimet.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class UserNameValidator {

	public static boolean isUserNameValid ( String username ) throws Exception 
	{
		boolean ret	= true;
		
		/*
		 * 3 or more consecutive characters
		 */
		
		int highest	= 1;
		for ( int i = 0 ; i < username.length()-1 ; i++ )
		{
			int count	= 1;
			for ( int j = i+1 ; j < username.length() ; j++ )
			{
				char c1 	= username.charAt(i);
				char c2 	= username.charAt(j);
				if ( c1 == c2 )
					count++;
				else 
				{
					if ( count > highest )
						highest	= count;
					count	= 1;
					i = j;
				}
			}
			if ( count > 1 && count > highest )
				highest	= count;
		}
		
		if ( highest > 2 )
			return false;
		
		
		/*
		 * Only integers
		 */
		
		try {
			ret = false;
			Integer.parseInt(username);
		}
		catch (Exception e) {
			ret = true;
		}
		
		if ( !ret )
			return ret;
		
		/*
		 * ends with dot
		 */
		
		if ( username.endsWith(".") )
			return false;
		
		/*
		 * starts with dot
		 */
		
		if ( username.startsWith(".") )
			return false;
		
		/*
		 * has more than 1 dot
		 */
		
		if(username.indexOf('.', username.indexOf('.') + 1) != -1) 
			return false;
		
		
		/*
		 * 4 or more consecutive vowel
		 */
		highest	= 1;
		for ( int i = 0 ; i < username.length()-1 ; i++ )
		{
			int count	= 1;
			for ( int j = i+1 ; j < username.length() ; j++ )
			{
				char c1 	= username.charAt(i);
				String w1	= (Character.toString(c1)).replaceAll("[aeıioöuü]", "");
				char c2 	= username.charAt(j);
				String w2	= (Character.toString(c2)).replaceAll("[aeıioöuü]", "");
				if ( w1.length() == 0 && w2.length() == 0 )
					count++;
				else 
				{
					if ( count > highest )
						highest	= count;
					count	= 1;
					i = j;
				}
			}
			if ( count > 1 && count > highest )
				highest	= count;
		}
		
		if ( highest > 3 )
			return false;
		
		/*
		 * 4 or more consecutive non-vowel
		 */
		highest	= 1;
		for ( int i = 0 ; i < username.length()-1 ; i++ )
		{
			int count	= 1;
			for ( int j = i+1 ; j < username.length() ; j++ )
			{
				char c1 	= username.charAt(i);
				String w1	= (Character.toString(c1)).replaceAll("[^aeıioöuü_.]", "");
				char c2 	= username.charAt(j);
				String w2	= (Character.toString(c2)).replaceAll("[^aeıioöuü_.]", "");
				if ( w1.length() == 0 && w2.length() == 0 )
					count++;
				else 
				{
					if ( count > highest )
						highest	= count;
					count	= 1;
					i = j;
				}
			}
			if ( count > 1 && count > highest )
				highest	= count;
		}
		
		if ( highest > 3 )
			return false;
		
		
		return ret;
	}
	
	public static String toTitleCase( String inStr ) {
		
		// Build a list of all the letters in the string:
		List<String> letterList = new ArrayList<String>();
        for ( int i = 0; i < inStr.length(); i++ ) {
        	letterList.add(inStr.substring(i,(i+1)));
        }
        
	    // Build a new string letter by letter in outStr:
	    String outStr = "";
	    String strThisLetter = "";
	    String strPreviousLetter = " ";
	    
	    // Iterate through the list one letter at a time:
	    Iterator<String> iter = letterList.iterator();

        while ( iter.hasNext() ) {
        	strThisLetter = ( String ) iter.next();
        	
        	// Was the previous letter a space?
            if ( strPreviousLetter.equals(" ") == true ) 
            {
            	// Yes, convert this letter to upper case:
            	outStr += strThisLetter.toUpperCase(new Locale("tr","TR"));
            } 
            else 
            {
            	// No, leave this letter as it is:
            	outStr += strThisLetter;
            }
            strPreviousLetter = strThisLetter;
        }
        
        return outStr;
	}
}
